﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;

namespace CIS470MetricsTracking
{
	public partial class CreateProject : Form
	{
		ProjectSelection proj_sel;
		public CreateProject(ProjectSelection proj_sel)
		{
			InitializeComponent();

			this.proj_sel = proj_sel;
		}

		private void btnCreateProject_Click(object sender, EventArgs e)
		{
			if (!validate_date(txtStartDate.Text) || txtStartDate.Text.Equals("") ||
				txtStartDate.Text.StartsWith(" ")) {
				lblErrorText.Text = "Invalid Start Date.";
				lblErrorText.Visible = true;
				return;
			}

			if (!validate_date(txtEndDate.Text) || txtEndDate.Text.Equals("") ||
				txtEndDate.Text.StartsWith(" ")) {
				lblErrorText.Text = "Invalid End Date.";
				lblErrorText.Visible = true;
				return;
			}

			if (!ControllerManager.project_controller.validate_project(txtProjectName.Text) ||
				txtProjectName.Text.Equals("") || txtProjectName.Text.StartsWith(" ")) {
				lblErrorText.Text = "Project name is either empty or already exists";
				lblErrorText.Visible = true;
				return;
			}

			if (txtDescription.Text.Equals("") || txtDescription.Text.StartsWith(" ")) {
				lblErrorText.Text = "Invalid Project Description.";
				lblErrorText.Visible = true;
			}

			ControllerManager.project_controller.create_project(txtProjectName.Text, txtStartDate.Text,
				txtEndDate.Text, txtDescription.Text);

			
			this.Close();			
		}

		// Return true if we have a valid date
		private bool validate_date(string date)
		{
			DateTime temp;
			if (!DateTime.TryParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture,
						DateTimeStyles.None, out temp)) {
				return false;
			} else {
				return true;
			}
		}

		private void CreateProject_Load(object sender, EventArgs e)
		{

		}

		private void CreateProject_FormClosing(object sender, FormClosingEventArgs e)
		{
			proj_sel.Show();
			proj_sel.BringToFront();
		}
	}
}
